package one.bugu.android.demon.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.List;

/**
 * 关于APK工具类
 */
public class APKUtils {

    /**
     * 单例对象
     */
    private static volatile APKUtils singleton = null;

    private APKUtils() {

    }

    /**
     * 获取单例对象
     *
     * @return 单例对象
     */
    public static APKUtils getInstance() {
        if (singleton == null) {
            synchronized (APKUtils.class) {
                if (singleton == null) {
                    singleton = new APKUtils();
                }
            }
        }
        return singleton;
    }

    /**
     * 是否安装
     *
     * @param pageName
     * @return
     */
    public boolean isInstallApk(Context context, String pageName) {
        // 获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals(pageName)) {
                    return true;
                }
            }
        }
        return false;
    }
}
